package top.lbqaq.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author luoboQAQ
 * @Date 2021/11/14
 */
public class BaseDao {
    private static final String DRIVER;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;

    // 获取数据库连接配置
    static {
        Properties properties = new Properties();
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");

        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        DRIVER = properties.getProperty("driver");
        URL = properties.getProperty("url");
        USERNAME = properties.getProperty("username");
        PASSWORD = properties.getProperty("password");
    }

    /**
     * 获取数据库连接
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 查询操作
     *
     * @param connection 数据库连接
     * @param pstm       动态SQL
     * @param rs         SQL结果集
     * @param sql        欲执行的SQL语句
     * @param params     欲执行SQL语句中的参数
     * @return SQL结果集
     */
    public static ResultSet execute(Connection connection, PreparedStatement pstm, ResultSet rs,
                                    String sql, Object[] params) throws SQLException {
        pstm = connection.prepareStatement(sql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
        }
        rs = pstm.executeQuery();
        return rs;
    }

    /**
     * 查询操作
     *
     * @param connection 数据库连接
     * @param pstm       动态SQL
     * @param rs         SQL结果集
     * @param sql        欲执行的SQL语句
     * @return SQL结果集
     */
    public static ResultSet execute(Connection connection, PreparedStatement pstm, ResultSet rs,
                                    String sql) throws SQLException {
        pstm = connection.prepareStatement(sql);
        rs = pstm.executeQuery();
        return rs;
    }

    /**
     * 增删改操作
     *
     * @param connection 数据库连接
     * @param pstm       动态SQL
     * @param sql        欲执行的SQL语句
     * @param params     欲执行SQL语句中的参数
     * @return 影响的行数
     */
    public static int execute(Connection connection, PreparedStatement pstm,
                              String sql, Object[] params) throws SQLException {
        int updateRows = 0;
        pstm = connection.prepareStatement(sql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                pstm.setObject(i + 1, params[i]);
            }
        }
        updateRows = pstm.executeUpdate();
        return updateRows;
    }

    /**
     * 释放资源
     *
     * @param connection 数据库连接
     * @param pstm       动态SQL
     * @param rs         SQL结果集
     * @return 是否释放成功
     */
    public static boolean closeResource(Connection connection, PreparedStatement pstm, ResultSet rs) {
        boolean flag = true;
        if (rs != null) {
            try {
                rs.close();
                // help GC
                rs = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        if (pstm != null) {
            try {
                pstm.close();
                // help GC
                pstm = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        if (connection != null) {
            try {
                connection.close();
                // help GC
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }

}
